package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.item.TbItemCat;
import com.dongdongshop.service.GoodsServiceI;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Service
public class GoodsServiceImpl implements GoodsServiceI {
    @Resource
    private TbGoodsMapper goodsMapper;
    @Resource
    private TbGoodsDescMapper goodsDescMapper;
    @Resource
    private TbItemMapper itemMapper;

    @Override
    public int updateStatus(Long[] ids, String flag) {
        return goodsMapper.updateStatus(ids,flag);
    }

    @Override
    public List<TbGoods> listGood() {
        return goodsMapper.selectByExample(null);
    }

    @Override
    public List<TbGoods> getGoodsAll() {
        return goodsMapper.selectByExample(null);
    }

    @Override
    public TbGoods getGoodsId(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public int insertGoods(TbGoods goods, TbGoodsDesc goodsDesc, String sellerName, List<TbItem> lists) {
      try {
          System.out.println("进入方法"+lists.size());
          //增加goods表 在返回id
          goodsMapper.insertGoodsById(goods);
          System.out.println("id"+goods.getId());
          //增加goods_desc表
          goodsDesc.setGoodsId(goods.getId());
          goodsDescMapper.insertGoodsDesc(goodsDesc);
          //增加item表
          for (TbItem tbItem :lists) {
              System.out.println("item"+tbItem);
              tbItem.setCreateTime(new Date());
              tbItem.setUpdateTime(new Date());
              tbItem.setGoodsId(goods.getId());
              tbItem.setSellerId(goods.getSellerId());
              tbItem.setSeller(sellerName);
              itemMapper.insertSelective(tbItem);
          }
          return 1;
      }catch (Exception e){
          return 0;
      }

    }
}
